<html>
<head><meta charset="utf-8"><title>Pin down type to crate version · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html">Pin down type to crate version</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234354246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234354246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234354246">(Apr 13 2021 at 16:03)</a>:</h4>
<p>Is there a trick, how one could enforce that only a specific version of a type from an external crate can be used? Version meaning the version of the crate. This would be like pinning down this specific type, bound to this version of the crate.<br>
So if the crate got updated (and potentially also the fields of the type), I would have to manually opt-in to use the new type (otherwise compilation should fail).</p>



<a name="234356122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234356122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Vossen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234356122">(Apr 13 2021 at 16:15)</a>:</h4>
<p>Do you mean just pinning the version of the crate? <a href="https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html">https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html</a></p>



<a name="234362072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234362072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234362072">(Apr 13 2021 at 16:50)</a>:</h4>
<p><span class="user-mention" data-user-id="246502">@Jake Vossen</span> No, this is not what I meant.<br>
I want  some trick which forces me to acknowledge _in code_, that the fields of a type from an external crate might have changed, after an update to said crate.</p>



<a name="234362552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234362552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Vossen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234362552">(Apr 13 2021 at 16:54)</a>:</h4>
<p>I mean, if you where using a type from a crate, then the type changed when you updated in a non-backwards compatible way, wouldn't your code fail to compile? Sorry I don't think I am following with what you are trying to do :/</p>



<a name="234363231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363231">(Apr 13 2021 at 16:58)</a>:</h4>
<p>For some further context:<br>
This question is related to the  bytemuck crate.<br>
I want to implement <code>Pod</code> and <code>Zeroable</code> (foreign types from external crate) for <code>foreign::Type</code> (type from a different external crate). This is normally not possible, because Rust prohibits the implementation of foreign traits for foreign types.<br>
But using <code>TransparentWrapper</code> (also part of bytemuck) I can write a <code>#[repr(transparent)]</code> wrapper struct around <code>foreign::Type</code> which allows me to safely transmute between my wrapper and the inner type. This now allows me to implement <code>Pod</code> and <code>Zeroable</code> on my wrapper struct and still be able to use all the functions of bytemuck with <code>foreign::Type</code>.<br>
The problem now is that, if I update the <code>foreign</code> dependency, the fields/representation of <code>foreign::Type</code> might have changed, which might make it no longer eligible for the unsafe contract guarding <code>Pod</code> and <code>Zeroable</code>.</p>



<a name="234363501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363501">(Apr 13 2021 at 17:00)</a>:</h4>
<p>Therefore I would like some trick to have to opt-in to a version change of <code>foreign::Type</code>.</p>



<a name="234363536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Vossen <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363536">(Apr 13 2021 at 17:00)</a>:</h4>
<p>Ah, I see the problem now. Yeah, this is over my head, maybe someone else here can help <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="234363697"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363697" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363697">(Apr 13 2021 at 17:00)</a>:</h4>
<p>it still seems to me that pinning the version in <code>Cargo.toml</code> is correct</p>



<a name="234363713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363713">(Apr 13 2021 at 17:00)</a>:</h4>
<p>rustc doesn't really know about versions</p>



<a name="234363970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234363970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234363970">(Apr 13 2021 at 17:02)</a>:</h4>
<p>related PR, where this discussion came up: <a href="https://github.com/Lokathor/bytemuck/pull/58#discussion_r601104887">https://github.com/Lokathor/bytemuck/pull/58#discussion_r601104887</a></p>



<a name="234364244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234364244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234364244">(Apr 13 2021 at 17:04)</a>:</h4>
<p>It might very well be, that there is no way to do this. But maybe there is some trick, to enforce this.</p>



<a name="234367818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234367818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234367818">(Apr 13 2021 at 17:26)</a>:</h4>
<p><span class="user-mention" data-user-id="311029">@Luis Wirth</span> does the foreign type implement <code>Serialize</code>? You could serialize one version to JSON, and then try to deserialize it from your build script, serde might complain if it has extra fields or  lost fields?</p>



<a name="234368003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234368003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234368003">(Apr 13 2021 at 17:27)</a>:</h4>
<p>but hmm i guess that wont detect changes in the <code>#[repr()]</code> of the foreign type <span aria-label="sad" class="emoji emoji-2639" role="img" title="sad">:sad:</span></p>



<a name="234368060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234368060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234368060">(Apr 13 2021 at 17:27)</a>:</h4>
<p>it's a bit of a tangent, but I would consider serialization changes to be semver-breaking, even for otherwise private fields</p>



<a name="234368390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234368390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234368390">(Apr 13 2021 at 17:29)</a>:</h4>
<p>there could still be a bug in the code that unintentionally changes the layout of a type with a new patch version, for the really paranoid :P</p>



<a name="234368582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234368582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234368582">(Apr 13 2021 at 17:30)</a>:</h4>
<p><span class="user-mention" data-user-id="311029">@Luis Wirth</span> would a <code>#[test]</code> case be sufficient for you in order to detect if foreign::Type can safely implement <code>Pod</code>? If so, then  I might have an idea</p>



<a name="234371346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234371346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234371346">(Apr 13 2021 at 17:48)</a>:</h4>
<p><span class="user-mention" data-user-id="311029">@Luis Wirth</span>  If <code>foreign::Type</code> implements the <code>Default</code> trait or has a <code>new()</code> constructor, then you could do  something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">mem</span>::<span class="n">ManuallyDrop</span><span class="p">;</span><span class="w"></span>

<span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="cp">#[derive(Default)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">ForeignType</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">ensure_uninhabited</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="c1">// comment out to create an inhabited type</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="n">some_other_value</span>: <span class="kt">u64</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">const</span><span class="w"> </span><span class="n">FOREIGN_SIZE</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="n">core</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">ForeignType</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>

<span class="cp">#[repr(C)]</span><span class="w"></span>
<span class="k">union</span> <span class="nc">CoalMine</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="n">foreign_type</span>: <span class="nc">ManuallyDrop</span><span class="o">&lt;</span><span class="n">ForeignType</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="w">   </span><span class="n">canary</span>: <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">FOREIGN_SIZE</span><span class="p">],</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[test]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">detect_leaky_pod</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">coal_mine</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CoalMine</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">foreign_type</span>: <span class="nc">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="n">ForeignType</span>::<span class="n">default</span><span class="p">())</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">   </span><span class="fm">assert_eq!</span><span class="p">([</span><span class="mi">0</span><span class="k">u8</span><span class="p">;</span><span class="w"> </span><span class="n">FOREIGN_SIZE</span><span class="p">],</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">coal_mine</span><span class="p">.</span><span class="n">canary</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>If you run this in Miri, it will fail the test case and warn you whenever the struct does not actually implement <code>Pod</code> properly <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span>  Try it out on the <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7044b2c29018a0c3bfdd8fb5c6aa5b22">playground</a> by going to Tools &gt; Miri.</p>



<a name="234374796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234374796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234374796">(Apr 13 2021 at 18:11)</a>:</h4>
<p>build script that parses your cargo.toml or cargo.lock, spits out the version of the dependency as an environment / config variable, then an <code>assert_eq!(THE_VERSION_FROM_BUILD, "the expected version")</code> right next to your implementation :-)</p>



<a name="234375768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234375768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234375768">(Apr 13 2021 at 18:17)</a>:</h4>
<p>If the fields are pub you could also take addr_of on each field and assert offsets + some size_of/align_of checks.</p>



<a name="234376938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234376938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234376938">(Apr 13 2021 at 18:25)</a>:</h4>
<p><span class="user-mention" data-user-id="400735">@Pointerbender</span> Very interesting. Can you elaborate a bit more. I'm still trying to parse your code <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> . Why does Miri report UB? Isn't <code>FOREIGN_SIZE</code> the correct size? And what's the "(un)inhabited" stuff about?<br>
This only checks the sum of the sizes of the fields, right?</p>



<a name="234377166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234377166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234377166">(Apr 13 2021 at 18:26)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Ahh, cool. That's a nice and easy fix, which also scales well.</p>



<a name="234377315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234377315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234377315">(Apr 13 2021 at 18:27)</a>:</h4>
<p><span class="user-mention" data-user-id="330154">@The 8472</span> Also a nice idea! This is a good way to make sure that you are very aware of the layout</p>



<a name="234384990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234384990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234384990">(Apr 13 2021 at 19:19)</a>:</h4>
<p><span class="user-mention" data-user-id="311029">@Luis Wirth</span>  <code>FOREIGN_SIZE</code> is indeed the correct size, but the individual fields also have certain alignment requirements. If the size of one field is not perfectly aligned with the start of the following (aligned) field, then Rust inserts some implicit padding bytes in between the fields in order to properly align the next field. A type is said to be "inhabited" if there exist no such implicit padding bytes and all the possible byte values are valid for the given type (the mere absence of implicit padding bytes, does not imply that all its byte values are valid! An <code>enum Example { A, B}</code> uses all its bytes, but a byte value of <code>5</code> is invalid, for example). The <a href="https://docs.rs/bytemuck/1.5.1/bytemuck/trait.Pod.html">crate docs</a> for <code>Pod</code> also have some interesting things to say about this:</p>
<blockquote>
<p>Safety</p>
<ul>
<li>The type must be inhabited (eg: no Infallible).</li>
<li>The type must allow any bit pattern (eg: no bool or char, which have illegal bit patterns).</li>
<li>The type must not contain any padding bytes, either in the middle or on the end (eg: no #[repr(C)] struct Foo(u8, u16), which has padding in the middle, and also no #[repr(C)] struct Foo(u16, u8), which has padding on the end).<br>
...</li>
</ul>
</blockquote>
<p>Note that my suggestion only checks for implicit byte padding, it doesn't ensure the type is "fully inhabited". You could combine it with <span class="user-mention" data-user-id="330154">@The 8472</span> 's suggestion to also explicitly check the type of all the fields, e.g. <code>let field1: *const u64 = core::ptr::addr_of!(foreign_struct.field1)</code> in addition to the check for implicit padding and a check for a hard-coded expected size. Then manually check once that all field types are "inhabited". If the field type then changes (or the struct size changes if a new field is added), you will get a compile error (or a <code>#[test]</code> error in the latter case). Repeat this process for every field of the foreign struct which is not an inhabited language primitive. Unfortunately you can only ensure the types of public fields this way, if the struct has private fields, then you can't be sure that all it's values are valid (and even though a private field might be a <code>u32</code>, the invariant of the foreign type might say that it never stores anything bigger than 24 bits, for example, which also makes it invalid to assume it is inhabited). I hope I didn't make it sound too complicated, but to summarize: the foreign type must not have any implicit padding (my example checks for this), all its fields must be public, the size of the foreign type must be fixed (as part of your test) and all its fields must be of a type that is valid for every possible combination of bits (i.e. "inhabited").</p>



<a name="234387091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234387091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234387091">(Apr 13 2021 at 19:33)</a>:</h4>
<p>This page shows the sizes for the Rust language primitives: <a href="https://doc.rust-lang.org/reference/type-layout.html">https://doc.rust-lang.org/reference/type-layout.html</a> The alignment is not guaranteed to be the same on every platform, so a test with Miri may pass on your computer, but then still fail on a different target platform <span aria-label="sad" class="emoji emoji-2639" role="img" title="sad">:sad:</span></p>



<a name="234387349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234387349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234387349">(Apr 13 2021 at 19:35)</a>:</h4>
<p>An example of that is <code>#[repr(C)] struct Example(u32, u64)</code> which has no implicit padding on x86 platforms, but on platforms where <code>u64</code> is aligned to 64 bits, the compiler inserts 32 bits of implicit padding between the <code>u32</code> and the <code>u64</code>.</p>



<a name="234388217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234388217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234388217">(Apr 13 2021 at 19:42)</a>:</h4>
<p>Actually, I think I  have to correct myself there, the <code>#[repr(C)]</code> in this case ensure that the u64 is aligned to 64 bits, so it always has implicit padding, I think. This is getting more and more complicated, apologies! The <code>Pod</code> trait docs mean business when it says " it's a relatively strong claim to make about a type. Do not add [Pod] to your type casually." <span aria-label="stuck out tongue" class="emoji emoji-1f61b" role="img" title="stuck out tongue">:stuck_out_tongue:</span></p>



<a name="234392449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234392449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234392449">(Apr 13 2021 at 20:08)</a>:</h4>
<p>For <code>Zeroable</code> you could just test the bit pattern of a  <code>None</code> for <code>Option&lt;foreign&gt;</code>, no?</p>



<a name="234392588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234392588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234392588">(Apr 13 2021 at 20:09)</a>:</h4>
<p>wait, no, one could just use that to check for a niche, which we don't want.</p>



<a name="234393704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234393704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234393704">(Apr 13 2021 at 20:18)</a>:</h4>
<p>For <code>Zeroable</code> there is an (unstable) intrinsic available that could perhaps help out: <a href="https://doc.rust-lang.org/std/intrinsics/fn.assert_zero_valid.html">https://doc.rust-lang.org/std/intrinsics/fn.assert_zero_valid.html</a></p>



<a name="234439338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234439338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234439338">(Apr 14 2021 at 04:26)</a>:</h4>
<blockquote>
<p>A type is said to be "inhabited" if there exist no such implicit padding bytes and all the possible byte values are valid for the given type</p>
</blockquote>
<p><span class="user-mention" data-user-id="400735">@Pointerbender</span> That's not what inhabited means. An uninhabited type is one that has no valid elements; the never type (<code>!</code>), and empty enums like <code>Infallible</code> or <code>enum MyEmpty {}</code> are uninhabited, as well as tuples containing uninhabited types and so on. Anything else is inhabited. A type that implements <code>Default</code> is necessarily inhabited, but there are also types with no natural default that may still be nonempty, like <code>fn(u8)</code> or <code>&amp;'a ()</code>.</p>



<a name="234446567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234446567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Pointerbender <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234446567">(Apr 14 2021 at 06:15)</a>:</h4>
<p>Oh I see, that is good to know! Thanks! <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="234535239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234535239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234535239">(Apr 14 2021 at 16:56)</a>:</h4>
<p><span class="user-mention" data-user-id="400735">@Pointerbender</span> Thanks for the explanation! I get it now :).</p>



<a name="234535322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/234535322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Luis Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#234535322">(Apr 14 2021 at 16:56)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> Thanks for the correction :).</p>



<a name="236681450"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Pin%20down%20type%20to%20crate%20version/near/236681450" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tavian Barnes <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version.html#236681450">(Apr 29 2021 at 14:25)</a>:</h4>
<p><span class="user-mention silent" data-user-id="271719">Mario Carneiro</span> <a href="#narrow/stream/122651-general/topic/Pin.20down.20type.20to.20crate.20version/near/234439338">said</a>:</p>
<blockquote>
<p>A type that implements <code>Default</code> is necessarily inhabited</p>
</blockquote>
<p>Not really:</p>
<div class="codehilite"><pre><span></span><code>enum Uninhabited {}

impl Default for Uninhabited {
    fn default() -&gt; Self {
        panic!();
    }
}
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>